﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace NetCoreClass
{
    /// <summary>
    /// 后台运行程序
    /// </summary>
    public class ProcessUtil
    {
        /// <summary>
        /// Linux下运行命令
        /// </summary>
        /// <param name="cmd">命令行</param>
        /// <returns></returns>
        public static string LinuxProcess(string cmd)
        {
            Process p = new Process();
            p.StartInfo.FileName = "sh";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(cmd);
            p.StandardInput.WriteLine("exit");
            p.WaitForExit();
            string strResult = p.StandardOutput.ReadToEnd();

            p.Close();
            return strResult;
        }
    }
}
